import { DataRegister } from './../models';
import { Injectable } from '@angular/core';
import { BaseService } from './base.service';
import { DataRPermits } from '../models/dataRPermits';

@Injectable({
  providedIn: 'root'
})
export class DataRegisterService extends BaseService {

  tempDataRegisterId = '';
  /**
   * 创建dataRegister
   * @param dataRegister 数据
   * @param datarPermits 权限
   */
  createDataRegister(dataRegister: DataRegister, datarPermits: DataRPermits[] = []) {
    const url = 'api/dataRegister';
    return this.post(url, {
      dataRegister,
      datarPermits
    });
  }
  /**
   * 批量创建dataRegister
   * @param params 数据
   */
  createDataRegsiters(params: {
    dataRegister: DataRegister;
    datarPermits: DataRPermits[];
  }[]): Promise<{ datarPermits: any[]; dataRegister: DataRegister }[]> {
    const url = 'api/dataRegister/list';
    return this.post(url, params);
  }

  updateDataRegisterStatus(dataRegisterId: string, status: string) {
    const url = `api/dataRegister/${dataRegisterId}/status/${status}`;
    return this.post(url, {});
  }

  getDataRegister(dataRegisterId: string): Promise<DataRegister> {
    const url = `api/dataRegister/${dataRegisterId}`;
    return this.get(url);
  }

  deleteDataRegister(dataRegisterId: string) {
    const url = `api/dataRegister/${dataRegisterId}`;
    return this.delete(url);
  }

  getComponentData(params: {
    dataType: string
  }) {
    const url = 'api/data';
    return this.get(url, { params });
  }

  getDataSourceById(dsId: string) {
    const url = 'api/dataSource/' + dsId;
    return this.get(url);
  }

  saveAssemble(params: { assemble: any[]; dataSource: any }) {
    const url = 'api/dataSource/assemble';
    return this.post(url, params);
  }

  exportData(params: {
    body: any;
    queryParams: string;
  }) {
    let url = 'api/data/export';
    if (params.queryParams) {
      url = `${url}?${params.queryParams}`;
    }
    return this.post(url, params.body, { responseType: 'blob' as 'json' });
  }

  getHolidays(params?: {
    BeginDay?: string;
    EndDay?: string;
    Year?: number; // 2021
    Month?: string; // 2021-01
    IsHoliday?: 0 | 1;
  }) {
    const url = 'http://filegennet5.zepride.com/Tools/GetHoliday';
    return this.getByOriginUrl(url, { params: params as any });
  }

  syncDataToSAupplierSys(data) {
    const url = 'http://filegennet5.zepride.com/YouJianDataSync/SyncProjectData';
    return this.post(url, data, { absolutelyUrl: true });
  }
}
